home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Geode 1.5 / geode-latest.xpi / content / injected.js < prev    next >
Text File  |  2008-10-06  |  3KB  |  116 lines

  1. window.navigator.geolocation = {
  2.  
  3.     /* ----- nsIDOMGeoGeolocation ----- */
  4.  
  5.     get lastPosition() {
  6.         return this._lastPosition;
  7.     },
  8.  
  9.     getCurrentPosition : function(successCallback, errorCallback, options) {
  10.         if (!successCallback)
  11.             throw "successCallback argument to getCurrentPosition is required";
  12.         // "options" is ignored
  13.         var ID = this._geoGetterSerial++;
  14.         this._geoGetters[ID] = [successCallback, errorCallback];
  15.         this._geoGetterCount++;
  16.  
  17.         this._sendRequest("getPosition");
  18.     },
  19.  
  20.     watchPosition : function(successCallback, errorCallback, options) {
  21.         if (!successCallback)
  22.             throw "successCallback argument to watchPosition is required";
  23.  
  24.         // "options" is ignored
  25.         var ID = this._geoWatcherSerial++;
  26.         this._geoWatchers[ID] = [successCallback, errorCallback];
  27.  
  28.         // First watcher starts the ball rolling..
  29.         if (this._geoWatcherCount == 0) {
  30.             var self = this;
  31.             this._intervalID = setInterval(
  32.                                     function() {
  33.                                         self._sendRequest("watchPosition");
  34.                                     }, 30 * 1000);
  35.             this._sendRequest("watchPosition");
  36.         }
  37.         this._geoWatcherCount++;
  38.  
  39.         return ID;
  40.     },
  41.  
  42.     clearWatch : function (aID) {
  43.         if (!(aID in this._geoWatchers))
  44.             throw "invalid ID";
  45.  
  46.         delete this._geoWatchers[aID];
  47.         this._geoWatcherCount--;
  48.  
  49.         if (this._geoWatcherCount == 0) {
  50.             clearInterval(this._intervalID);
  51.             //this._sendRequest("stopWatchPosition");
  52.         }
  53.     },
  54.  
  55.  
  56.     /* ----- end of public API ----- */
  57.  
  58.  
  59.     _lastPosition : null,
  60.  
  61.     _geoGetters: {},
  62.     _geoGetterCount  : 0,
  63.     _geoGetterSerial : 0,
  64.  
  65.     _geoWatchers : {},
  66.     _geoWatcherCount  : 0,
  67.     _geoWatcherSerial : 0,
  68.  
  69.     _intervalID : null,
  70.  
  71.     _sendRequest : function (type, ID) {
  72.         // window.addEventListener("geoLocationReply", this._eventListener, false);
  73.  
  74.         var event = document.createEvent("Events");
  75.         event.initEvent("x-geode-locationRequest-" + type, true, true);
  76.         window.dispatchEvent(event);
  77.     },
  78.  
  79.  
  80.     _notifyListeners : function(aPosition) {
  81.         var callbacks;
  82.  
  83.         if ("latitude" in aPosition)
  84.             this._lastPosition = aPosition;
  85.  
  86.         // The getCurrentPosition callbacks are one-shot, so call and remove.
  87.         for each (callbacks in this._geoGetters) {
  88.             // Use the error handler if the "position" is really an error.
  89.             var callback;
  90.             if ("latitude" in aPosition)
  91.                 callback = callbacks[0];
  92.             else
  93.                 callback = callbacks[1];
  94.  
  95.             try {
  96.                 callback(aPosition);
  97.             } catch (e) {}
  98.         }
  99.  
  100.         this._geoGetters = {};
  101.         this._geoGetterCount = 0;
  102.  
  103.         for each (callbacks in this._geoWatchers) {
  104.             // Use the error handler if the "position" is really an error.
  105.             var callback;
  106.             if ("latitude" in aPosition)
  107.                 callback = callbacks[0];
  108.             else
  109.                 callback = callbacks[1];
  110.             try {
  111.                 callback(aPosition);
  112.             } catch (e) {}
  113.         }
  114.     }
  115. };
  116.